package com.wanmait.wisdom.manage.controller;

import com.wanmait.wisdom.entity.House;
import com.wanmait.wisdom.entity.Office;
import com.wanmait.wisdom.entity.Owner;
import com.wanmait.wisdom.manage.service.ManageOwnerService;
import com.wanmait.wisdom.util.AjaxResult;
import org.apache.commons.io.FilenameUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/manage")
public class ManageOwnerController {
    @Resource
    private ManageOwnerService manageOwnerService;


    @PostMapping("findOwnerByCondition")
    public AjaxResult findOwnerByCondition(@RequestBody Owner owner, Integer pageNum, Date startTime, Date endTime){
        if (pageNum==null){
            pageNum =1;
        }
        return AjaxResult.success(manageOwnerService.findOwnerByCondition(owner,pageNum,startTime,endTime,5));
    }

    @GetMapping("/getOwnerCount")
    public AjaxResult getOwnerCount(){
        return AjaxResult.success(manageOwnerService.getOwnerCount());
    }

    @PostMapping("/insertOwner")
    public AjaxResult insertOwner(@RequestBody Owner owner){
        System.out.println(owner);
        manageOwnerService.save(owner);
        return AjaxResult.success();
    }

    @PostMapping("/updateOwner")
    public AjaxResult updateOwner(@RequestBody Owner owner){
        System.out.println(owner);
        return AjaxResult.success(manageOwnerService.updateOwner(owner));
    }

    @GetMapping("/selectAllOwner")
    public AjaxResult selectAllOwner(Integer pageNum, Integer pageSize){
        return AjaxResult.success(manageOwnerService.selectAllOwnerInfo(pageNum,pageSize));
    }

    @GetMapping("/selectOwnerById")
    public AjaxResult selectOwnerById(House house){
        System.out.println(house);
        return AjaxResult.success(manageOwnerService.selectOwnerById(house.getId()));
    }

//    //上传照片
//    @PostMapping("/uploadPic")
//    public AjaxResult uploadPic(MultipartFile file){
//        try {
//            String path = ResourceUtils.getURL("classpath:").getPath() +"/static/uploadPic";
//            String fileName = UUID.randomUUID().toString().replaceAll("-","")+"."+FilenameUtils.getExtension(file.getOriginalFilename());
//            file.transferTo(new File(path,fileName));
//            return AjaxResult.success("上传成功").put("fileName",fileName);
//        } catch (IOException e) {
//            return AjaxResult.error("上传失败");
//        }
//    }
//    //上传身份证正面
//    @PostMapping("/uploadCardPicFacade")
//    public AjaxResult uploadCardPicFacade(MultipartFile file){
//        try {
//            String path = ResourceUtils.getURL("classpath:").getPath()+"/static/uploadCardPicFacade";
//            String fileName = UUID.randomUUID().toString().replaceAll("-","")+"."+FilenameUtils.getExtension(file.getOriginalFilename());
//            file.transferTo(new File(path,fileName));
//            return AjaxResult.success("上传成功").put("fileName",fileName);
//        } catch (IOException e) {
//            return AjaxResult.error("上传失败");
//        }
//    }
//    //上传身份证反面
//    @PostMapping("/uploadCardPicBack")
//    public AjaxResult uploadCardPicBack(MultipartFile file){
//        try {
//            String path = ResourceUtils.getURL("classpath:").getPath()+"/static/uploadCardPicBack";
//            String fileName = UUID.randomUUID().toString().replaceAll("-","")+"."+ FilenameUtils.getExtension(file.getOriginalFilename());
//            file.transferTo(new File(path,fileName));
//            return AjaxResult.success("上传成功").put("fileName",fileName);
//        } catch (IOException e) {
//            return AjaxResult.error("上传失败");
//        }
//    }
}
